package com.wingsoft.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wingsoft.common.utils.IdGenerator.IdGeneratorFactory;
import com.wingsoft.common.utils.StringUtils;
import com.wingsoft.system.domain.SysLogininfor;
import com.wingsoft.system.mapper.SysLogininforMapper;
import com.wingsoft.system.service.ISysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 系统访问日志情况信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysLogininforServiceImpl implements ISysLogininforService
{

    @Autowired
    private SysLogininforMapper logininforMapper;

    @Autowired
    private IdGeneratorFactory idGeneratorFactory;


    /**
     * 新增系统登录日志
     * 
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor)
    {
        logininfor.setInfoId(idGeneratorFactory.getId(SysLogininfor.class));
        logininfor.setLoginTime(new Date());
        logininforMapper.insertLogininfor(logininfor);
    }

    /**
     * 查询系统登录日志集合
     * 
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor)
    {
//        return logininforMapper.selectLogininforList(logininfor);

        Map<String, Object> params = logininfor.getParams();
        Object beginTime = params.get("beginTime");
        LocalDateTime beginTimeLocalDateTime = null;
        if(Objects.nonNull(beginTime)){
            LocalDate beginTimeLocalDate = LocalDate.parse(beginTime.toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            beginTimeLocalDateTime = LocalDateTime.of(beginTimeLocalDate, LocalTime.MIN);
        }
        Object endTime = params.get("endTime");
        LocalDateTime endTimeLocalDateTime = null;
        if(Objects.nonNull(endTime)){
            LocalDate endTimeLocalDate = LocalDate.parse(endTime.toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            endTimeLocalDateTime = LocalDateTime.of(endTimeLocalDate, LocalTime.MAX);
        }
        return logininforMapper.selectList(Wrappers.<SysLogininfor>lambdaQuery()
                .like(StringUtils.isNotBlank(logininfor.getIpaddr()), SysLogininfor::getIpaddr, logininfor.getIpaddr())
                .eq(StringUtils.isNotBlank(logininfor.getStatus()), SysLogininfor::getStatus, logininfor.getStatus())
                .like(StringUtils.isNotBlank(logininfor.getUserName()), SysLogininfor::getUserName, logininfor.getUserName())
                .ge(Objects.nonNull(beginTimeLocalDateTime), SysLogininfor::getLoginTime, beginTimeLocalDateTime)
                .le(Objects.nonNull(endTimeLocalDateTime), SysLogininfor::getLoginTime, endTimeLocalDateTime)
                .orderByDesc(SysLogininfor::getInfoId)
        );
    }

    /**
     * 批量删除系统登录日志
     * 
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds)
    {
        return logininforMapper.deleteLogininforByIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        logininforMapper.cleanLogininfor();
    }
}
